/**
 * 
 * NOTICE -- YOU MUST LEAVE THIS NOTICE IN PLACE.  PER THE TERMS OF THE LICENSE BELOW, YOU MAY SUBLICENSE THIS SOFTWARE 
 * IN ANY WAY THAT DOES NOT CONFLICT WITH THIS LICENSE.
 *
 * Copyright (c) 2011, Paul Gribben <paul.gribben@gmail.com>
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the 
 *       documentation and/or other materials provided with the distribution.
 *	
 *	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
 *	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
 *	DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
 *	SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 *	WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 *	OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
package org.softwise.rss2kindle.adapters;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;

import org.softwise.rss2kindle.impl.HtmlLinkEntryWriter;
import org.softwise.rss2kindle.impl.HtmlLinkEntryWriter.ElementConverter;
import org.softwise.rss2kindle.impl.HtmlLinkEntryWriter.ElementConverter.ElemChecker;
import org.softwise.rss2kindle.impl.HtmlLinkEntryWriter.ElementConverter.ElemDef;
import org.softwise.rss2kindle.model.EntryWriter;
import org.softwise.rss2kindle.model.RssAdapter;
import org.softwise.rss2kindle.model.RssEntryTransformer;
import org.w3c.dom.Element;

public class BBCSportAdapter implements RssAdapter {

	// http://newsrss.bbc.co.uk/rss/sportonline_uk_edition/front_page/rss.xml
	Pattern urlMatchPattern = Pattern
			.compile("http.*?/newsrss.bbc.co.uk/rss/sport.*");

	@Override
	public EntryWriter getEntryWriter() throws Exception {
		final Transformer transformer = TransformerFactory.newInstance()
				.newTransformer();
		final Map<String, String> styles = new HashMap<String, String>();
		styles
				.put(
						"h3.subtitle",
						"display: block;font-size: 1.17em;font-weight: bold;margin: 1em 0px;");
		styles
				.put(
						"p.first",
						"display: block;font-size: 1.17em;margin: 1em 0px;color: #666;");

		return new HtmlLinkEntryWriter(transformer,
				"//h1 | //*[@class='storybody']/p",
				new ElementConverter[] { new ElementConverter(
						new ElemChecker() {
							@Override
							public boolean matches(Element testElem) {
								return valueMatches(testElem.getNodeName(),
										"h1");
							}
						}, new ElemDef[] { new ElemDef("h3", "class",
								"subtitle") }) }, styles, false);

	}

	@Override
	public boolean isRssConsumer(URL url) {
		return urlMatchPattern.matcher(url.toExternalForm()).matches();
	}

	private static boolean valueMatches(String s1, String s2) {
		return (s1 == null && s2 == null) || (s1 != null && s1.equals(s2));
	}

	@Override
	public RssEntryTransformer getRssTransformer() throws Exception {
		return null;
	}	
}
